package com.droidpark.android.tools.bar;




import com.droidpark.android.tools.bar.R;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DroidBar extends RelativeLayout {

	private ImageView appIcon;
	private TextView title;
	private ProgressBar progress;
	private LinearLayout actionButtons;
	
	public DroidBar(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		((Activity)getContext()).getLayoutInflater().inflate(R.layout.droidpark_actionbar, this);
		initViews();
	}
	
	public void initViews() {
		appIcon = (ImageView) findViewById(R.id.droidpark_actionbar_appIcon);
		progress = (ProgressBar) findViewById(R.id.droidpark_actionbar_progress);
		title = (TextView) findViewById(R.id.droidpark_actionbar_title);
		actionButtons = (LinearLayout) findViewById(R.id.droidpark_actionbar_actionButtons);
	}
	
	public void setAppIcon(int resId) {
		setAppIcon(resId, null);
	}
	
	public void setAppIcon(int resId, OnClickListener onClickListener) {
		appIcon.setImageResource(resId);
		appIcon.setVisibility(VISIBLE);
		appIcon.setOnClickListener(onClickListener);
	}

	public void setTitle(CharSequence text) {
		title.setText(text);
	}
	
	public void setTitle(int resId) {
		title.setText(resId);
	}
	
	public void showProgressBar() {
		progress.setVisibility(VISIBLE);
	}
	
	public void hideProgressBar() {
		progress.setVisibility(GONE);
	}
	
	public void addActionButton(int buttonIconResId, OnClickListener onClickListener) {
		View view = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.droidpark_actionbar_button, actionButtons, false);
		ImageButton button = (ImageButton) view.findViewById(R.id.droidpark_actionbar_button);
		button.setImageResource(buttonIconResId);
		button.setOnClickListener(onClickListener);
		actionButtons.addView(view, actionButtons.getChildCount());
	}
	
	public boolean removeActionButton(int index) {
		int count = actionButtons.getChildCount();
		if(count > 0 && index >= 0 && index < count) {
			actionButtons.removeViewAt(index);
			return true;
		}
		return false;
	}
	
	public boolean isProgressBarVisible() {
		return progress.getVisibility() == VISIBLE;
	}
	
	public void hideAppIcon() {
		appIcon.setVisibility(INVISIBLE);
	}
}
